local blaze = require "blaze"
local app = require "app"
local game = app.game.running
local consts = game.util.consts

local Text_type = typeof(CS.TMPro.TextMeshProUGUI)

local M = game.ui.element()

local log = game.logging.get("bet_operator")

local bet = {
    on_click = function(self)
        game.audio.post("选择下注档位")
        game.store.bet_amounts.set_index(self.index)
    end,

    on_select = function(self, b)
        self.selected_obj:SetActive(b)
        self.unselected_obj:SetActive(not b)
    end,

    on_release = function(self)
        self.selected_value_txt = nil
        self.unselected_value_txt = nil
        self.gameObject = nil
        self.btn.onClick:RemoveAllListeners()
        self.btn = nil
        self.selected_obj = nil
        self.unselected_obj = nil
    end,

    set_active = function(self, b)
        self.gameObject:SetActive(b)
    end,

    set_txt = function(self, value)
        self.selected_value_txt.text = app.i18n.number(value, "chip5")
        self.unselected_value_txt.text = app.i18n.number(value, "chip5")
    end,
}

local get_history_total = function(bet_history)
    if bet_history == nil then
        return 0
    end

    local total = 0
    for _, t in ipairs(bet_history) do
        total = total + t.amount
    end

    return total
end

local create_bet = function(btn, index)
    local t = {}
    t.gameObject = btn.gameObject
    t.selected_obj = t.gameObject.transform:Find("selected_obj").gameObject
    t.unselected_obj = t.gameObject.transform:Find("unselected_obj").gameObject
    t.index = index
    t.btn = btn
    btn.onClick:AddListener(function() t:on_click() end)
    t.selected_value_txt = t.selected_obj.transform:Find("bet_value_txt"):GetComponent(Text_type)
    t.unselected_value_txt = t.unselected_obj.transform:Find("bet_value_txt"):GetComponent(Text_type)
    setmetatable(t, {__index = bet})
    return t
end

M.store_watchers = {
    bet_amounts = {
        ["$"] = function(self, store, state, args)
            self:init(state.config)
            self:update_bet(state.selected_bet_index)
        end,
        
        ["初始化下注档位"] = function(self, store, state, args)
            self:init(state.config)
            self:update_bet(state.selected_bet_index)
        end,

        ["改变下注"] = function(self, store, state, args)
            self:update_bet(state.selected_bet_index)
        end
    },
    gameplay = {
        ["$"] = blaze.noop,

        ["阶段变更"] = function(self, store, state, args)
            if state.phase_id == consts.PHASE_BET then
                local is_active = #game.store.gameplay.state.bet_history > 0
                self.btn_continue.gameObject:SetActive(is_active)
                local total_amount = get_history_total(game.store.gameplay.state.bet_history)
                self.total_value_txt.text = game.i18n.number(total_amount, "chip5")
            else
                self.btn_continue.gameObject:SetActive(false)
            end
        end
    }
}

function M:ctor()
    self.res = "club.threecard.baccaratlib/ui/common.b:bet_operator"
    self.bets = {}
end

function M:on_injected(bind)
    bind("button.click", self.btn_continue, self.continue_ivk)    
    self.bets[1] = create_bet(self.bet_1, 1)
    self.bets[2] = create_bet(self.bet_2, 2)
    self.bets[3] = create_bet(self.bet_3, 3)
    self.bets[4] = create_bet(self.bet_4, 4)
    self.bets[5] = create_bet(self.bet_5, 5)

    game.utility.button_style.highlight_scale(self.btn_continue)
end

function M:init(bet_amounts)
    for index = 1, 5 do
        self.bets[index]:set_active(false)
    end

    for index, value in ipairs(bet_amounts) do
        self.bets[index]:set_active(true)
        self.bets[index]:set_txt(value)
    end
end

function M:update_bet(selected_index)
    for i = 1, 5 do
        self.bets[i]:on_select(i == selected_index)
    end
end

function M:continue_ivk()
    local history = game.store.gameplay.state.bet_history
    local total_amount = get_history_total(history)

    if total_amount > game.kbe.player().chip then
        game.toast.show("钱不够续投")
        return
    end

    self:start_coroutine(function()
        for _, t in ipairs(history) do
            game.kbe.player():bet(t.id, t.amount)
            coroutine.yield()
        end
    end)
end

function M:on_release()
    for _, bet in ipairs(self.bets) do
        bet:on_release()
    end
    self.bets = nil
end

return M